package com.uenpay.im.ui.old.activity.personal;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.qiyunxin.android.http.utils.FileUtils;
import com.qyx.android.weight.clip.ClipImageLayout;
import com.qyx.android.weight.utils.QyxWeightImageUtils;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;

/**
 *剪切
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class ClipImageActivity extends Activity {
	private static final String KEY = "picPath";
	private ClipImageLayout mClipImageLayout = null;
	FileUtils fileUtils = new FileUtils(IMApplication.appName);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.crop_image_layout);
		IMApplication.getInstance().addActivity(this);
		mClipImageLayout = (ClipImageLayout) findViewById(R.id.clipImageLayout);
		String path = getIntent().getStringExtra(KEY);
		/**
		 * 获取图片的旋转度
		 */
		int degreee = QyxWeightImageUtils.readBitmapDegree(path);
		Bitmap bitmap = QyxWeightImageUtils.getBitmapByPath(path);
		if (bitmap != null) {
			if (degreee == 0) {
				mClipImageLayout.setImageBitmap(bitmap);
			} else {
				mClipImageLayout.setImageBitmap(QyxWeightImageUtils.rotateBitmap(
						degreee, bitmap));
			}
		} else {
			finish();
		}
		initView();
	}

	private void initView() {
		((TextView) findViewById(R.id.title_textview))
				.setText(R.string.clip_picture);
		((TextView) findViewById(R.id.title_right_tv)).setText(R.string.sure);
		findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);

		findViewById(R.id.title_right_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						Bitmap bitmap = mClipImageLayout.clip();
						Intent intent = new Intent();
						String path = fileUtils.saveCropBitmap(bitmap);
						intent.putExtra("pic_path", path);
						setResult(RESULT_OK, intent);
						finish();
					}
				});
		findViewById(R.id.back_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						finish();
					}
				});
	}

	@Override
	protected void onDestroy() {
		IMApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}
}
